﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.DataObjects.Rules
{
    public class EntityImportRule : RuleBase
    {
        #region props
        private string _DataSourcePath;
        /// <summary></summary>
        public string DataSourcePath
        {
            get
            {
                return this._DataSourcePath;
            }
            set
            {
                this._DataSourcePath = value;
            }
        }

        private string _RecordListPath;
        /// <summary></summary>
        public string RecordListPath
        {
            get
            {
                return this._RecordListPath;
            }
            set
            {
                this._RecordListPath = value;
            }
        }


        private Dictionary<string,string> _EntityFieldSrcFieldMappings;
        /// <summary></summary>
        public Dictionary<string,string> EntityFieldSrcFieldMappings
        {
            get
            {
                return this._EntityFieldSrcFieldMappings;
            }
            set
            {
                this._EntityFieldSrcFieldMappings = value;
            }
        }
        #endregion

        public EntityImportRule(
            string ruleName, SetDataSourceType srcDataFormat,
            EntityType entityType, string iconPath):
            base(ruleName, Rules.RuleType.Import, 
            srcDataFormat, entityType, entityType, iconPath)
        {
            this._DataSourcePath = string.Empty;
            this._RecordListPath = string.Empty;
            this._EntityFieldSrcFieldMappings=new Dictionary<string, string>();
        }

        #region xml 
        public override void Save(ref XmlDocument xDoc, XmlNode ruleNode)
        {
            XmlDataUtil.UpdateAttribute(ref xDoc, ruleNode, "RuleName", this.RuleName);
            XmlDataUtil.UpdateAttribute(ref xDoc, ruleNode, "RuleType", this.RuleType.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, ruleNode, "IconPath", this.IconPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, ruleNode, "SrcDataFormat", this.SrcDataFormat.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, ruleNode, "DataSourcePath", this.DataSourcePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, ruleNode, "RecordListPath", this.RecordListPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, ruleNode, "TgtEntityType", this.TgtEntityType.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, ruleNode, "SrcEntityType", this.SrcEntityType.ToString());
            XmlNode mappingNodes = XmlDataUtil.AddElement(ref xDoc, ruleNode, "FieldMappings");
            if(this.EntityFieldSrcFieldMappings !=null && this.EntityFieldSrcFieldMappings.Count>0)
            {
                foreach(string propName in this.EntityFieldSrcFieldMappings.Keys)
                {
                    XmlNode mappingNode = XmlDataUtil.AddElement(ref xDoc, mappingNodes, "FieldMapping");
                    XmlDataUtil.UpdateAttribute(ref xDoc, mappingNode, "PropName", propName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, mappingNode, "SrcFieldName",
                                                this.EntityFieldSrcFieldMappings[propName]);
                }
            }
        }

        public override RuleBase Read(XmlNode ruleNode)
        {
            EntityImportRule importRule= 
                new EntityImportRule(string.Empty, SetDataSourceType.ExcelFile,
                    EntityType.Grants, string.Empty);
            importRule.RuleName = XmlDataUtil.GetAttributeValue(ruleNode, "RuleName", "");
            importRule.RuleType = (RuleType) Enum.Parse(
                typeof (RuleType), XmlDataUtil.GetAttributeValue(
                    ruleNode, "RuleType", RuleType.Import.ToString()));
            importRule.IconPath = XmlDataUtil.GetAttributeValue(ruleNode, "IconPath", "");
            importRule.SrcDataFormat =
                (SetDataSourceType)
                Enum.Parse(typeof (SetDataSourceType),
                           XmlDataUtil.GetAttributeValue(ruleNode, "SrcDataFormat", SetDataSourceType.DB.ToString()));
            importRule.DataSourcePath = XmlDataUtil.GetAttributeValue(ruleNode, "DataSourcePath", "");
            importRule.RecordListPath = XmlDataUtil.GetAttributeValue(ruleNode, "RecordListPath", "");
            importRule.SrcEntityType =
                (EntityType)
                Enum.Parse(
                    typeof (EntityType),
                    XmlDataUtil.GetAttributeValue(
                        ruleNode, "SrcEntityType",
                        EntityType.Grants.ToString()));
            importRule.TgtEntityType =
                (EntityType)
                Enum.Parse(
                    typeof(EntityType),
                    XmlDataUtil.GetAttributeValue(
                        ruleNode, "TgtEntityType",
                        EntityType.Grants.ToString()));
            importRule.EntityFieldSrcFieldMappings = new Dictionary<string, string>();
            XmlNodeList mappingNodes = ruleNode.SelectNodes("FieldMappings/FieldMapping");
            if(mappingNodes !=null && mappingNodes.Count>0)
            {
                foreach(XmlNode mappingNode in mappingNodes)
                {
                    string propName = XmlDataUtil.GetAttributeValue(mappingNode, "PropName", "");
                    string srcFieldName = XmlDataUtil.GetAttributeValue(mappingNode, "SrcFieldName", "");
                    importRule.EntityFieldSrcFieldMappings.Add(propName, srcFieldName);
                }
            }
            return importRule;
        }
        #endregion
    }
}
